package com.whpost.service.service.t;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whpost.service.dao.t.*;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.t.TFiles;
import com.whpost.service.entity.t.TMenu;
import com.whpost.service.entity.t.TWeek;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import com.whpost.service.tools.ToolBean;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@Service(value = "indexServ")
public class IndexServices extends BaseServices<TFilesMapper, TFiles> {

    @Resource
    private TMenuDao tMenuDao;
    @Resource
    private TRoleDao tRoleDao;

    public ResultInfo init(String loginId) throws UnsupportedEncodingException {
        TCzy tCzy = gettCzyDao().selectById(loginId);
        List<String> menuIds=new ArrayList<>();
        if (loginId.equals("1"))  // 超级管理员
        {
            menuIds = tMenuDao.getAllMenuId();
        }else{
            if (tCzy.getRoleId()!=null) {
                String menus = tRoleDao.getMenuByRoleId(tCzy.getRoleId()).replaceAll("r", "");
                menuIds = Arrays.asList(menus.split(","));
            }
        }

        //主菜单排个序号
        List<TMenu> tMenuList = tMenuDao.selectList(new QueryWrapper<TMenu>().eq("jb",1).in("id",menuIds).orderByAsc("xh"));

        Map<String,Object> menuMap = new HashMap<>();
        List<Map> mapList = new ArrayList<>();
        List<TMenu> submenuList = new ArrayList<TMenu>();
        for (int i=0;i<tMenuList.size();i++)
        {
            submenuList = new ArrayList<TMenu>();
            submenuList = tMenuDao.selectList(new QueryWrapper<TMenu>().eq("pid",tMenuList.get(i).getId()).eq("jb",2).in("id",menuIds).orderByAsc("xh"));
            menuMap = new HashMap<>();
            menuMap.put("menu",tMenuList.get(i));
            menuMap.put("submenu",submenuList);
            mapList.add(menuMap);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("menu",mapList);
        map.put("user",tCzy);
        ResultInfo resultInfo = new ResultInfo(0, "成功返回！",map);
        return backRtn(resultInfo,loginId);
    }
}
